<?php

namespace App\Models\Api;

use App\Models\Api\Base;
use Cache;

class AstroSetting extends Base
{
    protected $table   = 'astro_setting';

    const CACHE_KEY = 'user_astro_setting_';
    const CACHE_TIME = 86400 * 3;

    /**
     * 获取用户自定义星盘设置
     * @param array $map
     * @return array|bool|false|mixed|\PDOStatement|string|\think\Model
     */
    public function getUserSettingByTid($map = [])
    {
        $tid = $map['tid'];
        $uid = $map['uid'];
        $chart = $map['chart'];

        $setting = false;

        if ($this->cache_on >= 1 && Cache::has(self::CACHE_KEY . $uid . '-' . $chart . '-' . $tid)) {
            $setting = Cache::get(self::CACHE_KEY . $uid . '-' . $chart . '-' . $tid);
        }

        if ($setting === false) {
            try {
                $setting = $this
                    ->select('id', $chart)
                    ->where(['uid' => $uid, 'tid' => $tid])
                    ->firstOrFail();

                $setting = json_decode(json_encode($setting, true), true);

                if (!empty($setting[$chart])) {
                    $setting['is_default'] = false;

                    $setting[$chart] = json_decode($setting[$chart], true);
                } else {
                    $setting['is_default'] = true;
                }
            } catch (\Exception $e) {
                $setting['is_default'] = true;
                // 查询用户配置失败 使用默认配置
                $setting[$chart] = config('astroset.setting' . $tid)[$chart];
            }
            if (empty($setting[$chart])) {
                $setting[$chart] = config('astroset.setting' . $tid)[$chart];
            }

            if ($this->cache_on >= 1) {
                Cache::set(self::CACHE_KEY . $uid . '-' . $chart . '-' . $tid, $setting, self::CACHE_TIME);
            }
        } else {
            if (!is_array($setting[$chart])) {
                $setting[$chart] = json_decode($setting[$chart], true);
            }
        }
        return $setting;
    }

    public function updateUserSetting($map = [])
    {
        if (isset($map['id'])) {
            $data = [
                'id' => intval($map['id']),
                $map['chart'] => $map['chart_value'],
            ];

            $result = $this->update($data);
        } else {
            $data = [
                'uid' => $map['uid'],
                'tid' => $map['tid'],
                $map['chart'] => $map['chart_value'],
            ];
            $result = $this->create($data);
        }
        // 更新完成 更新缓存
        if ($this->cache_on >= 1) {
            Cache::set(self::CACHE_KEY . $map['uid'] . '-' . $map['chart'] . '-' . $map['tid'], $data, self::CACHE_TIME);
        }

        return $result;
    }

    /**
     * 星盘配置还原
     * @param array $map
     * @return bool
     */
    public function restoreUserSetting($map = [])
    {
        if (isset($map['id'])) {
            $where = ['id' => intval($map['id'])];
        } else {
            $where = ['uid' => $map['uid'], 'tid' => $map['tid']];
        }

        try {
            $this->where($where)->update([$map['chart'] => '']);
            // 更新完成 更新缓存
            if ($this->cache_on >= 1) {
                Cache::forget(self::CACHE_KEY . $map['uid'] . '-' . $map['chart'] . '-' . $map['tid']);
            }
        } catch (\Exception $e) {
            $this->error = $e->getMessage();

            return false;
        }
        return true;
    }
    
     /**
     * 获取用户自定义星盘设置
     * @param array $map
     * @return array|bool|false|mixed|\PDOStatement|string|\think\Model
     */
    public function get_user_setting($map = [])
    {
        $tid = $map['tid'];
        $uid = $map['uid'];
        $chart = $map['chart'];

        $setting = false;

        if ($this->cache_on >= 1)
        {
            $setting = Cache::get(self::CACHE_KEY . $uid . '-' . $chart . '-' . $tid);
        }

        if ($setting === false)
        {
            try
            {
                if ($tid >= 1 && $tid <= 3)
                {
                    $setting = $this
                        ->select('id','uid','use_time','update_time','tid', "$chart")
                        ->where("uid=$uid AND tid=$tid")
                        ->firstOrFail();
                } else
                {
                    $setting = $this
                        ->field('id','uid','use_time','update_time','tid',"$chart")
                        ->where("uid",'=',"$uid")
                        ->orderBy('use_time','DESC')
                        ->firstOrFail();
                }

                $setting = json_decode(json_encode($setting, true), true);
            }
            catch (\Exception $e)
            {
                // 查询用户配置失败 使用默认配置
                $setting = config('astroset.setting'.$tid)[$chart] ?? [];
            }
            if ($this->cache_on >= 1)
            {
                Cache::set(self::CACHE_KEY . $uid . '-' . $chart . '-' . $tid, $setting, self::CACHE_TIME);
            }
        }
        return $setting;
    }
}
